package com.mtiiot.fsmanage.server;


import com.mtiiot.fsmanage.bean.common.SysTreeNode;
import com.mtiiot.fscore.utils.StringUtil;
import com.mtiiot.fsmanage.core.dao.BaseElasticsearch;
import com.mtiiot.fsmanage.utils.Contants;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
@Transactional
public class CommonServer {
    @Autowired
    private BaseElasticsearch baseElasticsearch;
    private static String INDEX = Contants.INDEX_METADATA;
    //private static String TYPE = Contants.TYPE_METADATA_LOG;

    public Object getRootNode() throws Exception{
        List<SysTreeNode> list=new ArrayList<SysTreeNode>();
        SysTreeNode st=new SysTreeNode();
        st.setId("1");
        st.setText("系统");
        st.setChildren(true);
        list.add(st);
        return list;
    }
    public Object getChildrenNode(String id,String sysName,String l1Name,String l2Name,String type,String fileType) throws Exception {
        Client client=null;
        try{
            client=baseElasticsearch.getEsDataSource().getClient();
            SearchResponse sr=null;
            List<Terms.Bucket> list=null;
            List<SysTreeNode> sts=new ArrayList<SysTreeNode>();
            boolean hasChildren=true;
            if(id.equals("1")){
                //////////////////////查询系统////////////////
                if(StringUtil.isEmpty(fileType)){
                    sr = client.prepareSearch(INDEX)
                            .setTypes(type)
                            .setQuery(QueryBuilders.matchAllQuery())
                            .addAggregation(
                                    AggregationBuilders.terms("agg1").field("sysName")
                            ).execute().actionGet();
                }else{
                    sr = client.prepareSearch(INDEX)
                            .setTypes(type)
                            .setQuery(QueryBuilders.matchAllQuery())
                            .setQuery(QueryBuilders.termQuery("fileType", fileType))                 // Query
                            .addAggregation(
                                    AggregationBuilders.terms("agg1").field("sysName")
                            ).execute().actionGet();
                }
            }else if(id.split("-").length==2){
                //////////////////////查询L1////////////////
                if(StringUtil.isEmpty(fileType)){
                    sr = client.prepareSearch(INDEX)
                            .setTypes(type)
                            .setQuery(QueryBuilders.matchAllQuery())
                            .setQuery(QueryBuilders.termQuery("sysName", sysName))                 // Query
                            .addAggregation(
                                    AggregationBuilders.terms("agg1").field("l1Name")
                            ).execute().actionGet();
                }else {
                    sr = client.prepareSearch(INDEX)
                            .setTypes(type)
                            .setQuery(QueryBuilders.matchAllQuery())
                            .setQuery(QueryBuilders.termQuery("sysName", sysName))                 // Query
                            .setQuery(QueryBuilders.termQuery("fileType", fileType))                 // Query
                            .addAggregation(
                                    AggregationBuilders.terms("agg1").field("l1Name")
                            ).execute().actionGet();
                }

            }else if(id.split("-").length==3){
                //////////////////////查询L2////////////////
                if(StringUtil.isEmpty(fileType)){
                    sr = client.prepareSearch(INDEX)
                            .setTypes(type)
                            .setQuery(QueryBuilders.matchAllQuery())
                            .setQuery(QueryBuilders.termQuery("sysName", sysName))                 // Query
                            .setQuery(QueryBuilders.termQuery("l1Name", l1Name))                 // Query
                            .addAggregation(
                                    AggregationBuilders.terms("agg1").field("l2Name")
                            ).execute().actionGet();
                }else {
                    sr = client.prepareSearch(INDEX)
                            .setTypes(type)
                            .setQuery(QueryBuilders.matchAllQuery())
                            .setQuery(QueryBuilders.termQuery("sysName", sysName))                 // Query
                            .setQuery(QueryBuilders.termQuery("l1Name", l1Name))                 // Query
                            .setQuery(QueryBuilders.termQuery("fileType", fileType))                 // Query
                            .addAggregation(
                                    AggregationBuilders.terms("agg1").field("l2Name")
                            ).execute().actionGet();
                }

            }else if(id.split("-").length==4){
                //////////////////////查询L3////////////////1
                hasChildren=false;
                if(StringUtil.isEmpty(fileType)){
                    sr = client.prepareSearch(INDEX)
                            .setTypes(type)
                            .setQuery(QueryBuilders.matchAllQuery())
                            .setQuery(QueryBuilders.termQuery("sysName", sysName))                 // Query
                            .setQuery(QueryBuilders.termQuery("l1Name", l1Name))                 // Query
                            .setQuery(QueryBuilders.termQuery("l2Name", l2Name))                 // Query
                            .addAggregation(
                                    AggregationBuilders.terms("agg1").field("l3Name")
                            ).execute().actionGet();
                }else {
                    sr = client.prepareSearch(INDEX)
                            .setTypes(type)
                            .setQuery(QueryBuilders.matchAllQuery())
                            .setQuery(QueryBuilders.termQuery("sysName", sysName))                 // Query
                            .setQuery(QueryBuilders.termQuery("l1Name", l1Name))                 // Query
                            .setQuery(QueryBuilders.termQuery("l2Name", l2Name))                 // Query
                            .setQuery(QueryBuilders.termQuery("fileType", fileType))                 // Query
                            .addAggregation(
                                    AggregationBuilders.terms("agg1").field("l3Name")
                            ).execute().actionGet();
                }
            }
            Terms agg1 = sr.getAggregations().get("agg1");
            list=agg1.getBuckets();
            int i=1;
            Object key=null;
            String text=null;
            for(Terms.Bucket b:list){
                SysTreeNode st=new SysTreeNode();
                st.setId(id+"-"+i);
                key=b.getKey();
                text=key.toString();
                st.setText(text);
                st.setChildren(hasChildren);
                sts.add(st);
                i++;
            }
            return sts;
        }finally {
            if(client!=null){
                baseElasticsearch.getEsDataSource().releaseClient(client);
            }
        }
    }

}
